package com.zlx.mall.basic.authentication.property;


import com.zlx.mall.basic.common.exception.IllegalBizArgumentException;
import com.zlx.mall.basic.common.property.AbstractProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotNull;
import java.util.Map;
import java.util.Optional;

/**
 * @author qingke
 **/
@Slf4j
@Data
@EqualsAndHashCode(callSuper = true)
@Validated
@Component
@ConfigurationProperties(prefix = "hzl.app.authentication")
public class AuthenticationProperties extends AbstractProperty {

    @NotNull
    private Map<String, AuthenticationProperty> properties;

    public AuthenticationProperty getProperty(String name) {
        return Optional
                .ofNullable(properties.get(name))
                .orElseThrow(() -> new IllegalBizArgumentException("Unknown AuthenticationProperties, name: " + name));
    }

    @Override
    protected Logger getLogger() {
        return log;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        properties.forEach((name, property) -> log.info("> AuthenticationProperties, name: {}, property: {}", name, property));
    }
}
